import GameEvent from "../../Commons/GameEvent";
import SaveManager from "../../Managers/SaveManager";
import MapTemplate from "./MapTemplate";

export enum MapEvent {
    SELECTMAP_EVENT = "selectMap_event"
}

export default class MapManager implements IModelManager {

    private static _single: MapManager = null;
    private mapMap: Map<number, MapTemplate>;
    public static get single(): MapManager {
        if (MapManager._single == null) {
            MapManager._single = new MapManager();
        }
        return MapManager._single;
    }

    initConfig(): void {
        this.mapMap = new Map();
        var jsonAss = cc.resources.get("configs/map", cc.JsonAsset);

        for (const iterator of jsonAss.json) {
            var mapTp = new MapTemplate();
            mapTp.init(iterator);
            this.mapMap.set(mapTp.id, mapTp);
        }

    }


    getMapTemplates(): MapTemplate[] {
        var arr = [];
        this.mapMap.forEach((value: MapTemplate, key: number, map: Map<number, MapTemplate>) => {
            arr.push(value);
        }, this);

        return arr;
    }

    getMapTemplate(id: number): MapTemplate {
        return this.mapMap.get(id);
    }

    requestSelectMap(mapid: number) {
        var userdata = SaveManager.single.userdata;
        userdata.currentMapId = mapid;

        GameEvent.eventNode.emit(MapEvent.SELECTMAP_EVENT);
        SaveManager.single.saveData();
    }


}
